package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.constant.ErrConstant;
import com.hdax.dm.entity.user.DmUser;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.mappers.UserMapper;
import com.hdax.dm.service.RegisterService;
import com.hdax.dm.util.RandomCharacters;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RegisterSImpl extends ServiceImpl<UserMapper,DmUser> implements RegisterService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public CommonResponse<Integer> register(String password, String phone, String vcode) {
        DmUser user=new DmUser();
        user.setPassword(password);
        user.setPhone(phone);
        user.setNickName(RandomCharacters.getRandomCharacters(6));
        if(!vcode.equals(redisTemplate.opsForValue().get("vcode:"+phone))){
            throw new DmException(ErrConstant.ErrCodeConstant.WRONG_CODE_VCODE,ErrConstant.ErrMsgConstant.WRONG_MSG_VCODE);
        }
        return ResponseUtil.returnSuccess(baseMapper.insert(user));
    }
}
